﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PresensiClient.PegawaiSvc;
using PresensiClient.DepartmentSvc;
using PresensiClient.PresensiSvc;
using PresensiClient.IzinSvc;
using PresensiClient.LemburSvc;
using PresensiClient.Entity;
using PresensiClient.util;

namespace PresensiClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int operasi;

        private DepartmentService departmentSvc;
        private PegawaiService pegawaiSvc;
        private KehadiranConnector kehadiranConnector;
        private IzinConnector izinConnector;
        private LemburConnector lemburConnector;

        public MainWindow()
        {
            
            InitializeComponent();
            
            departmentSvc = new DepartmentSvc.DepartmentService();
            pegawaiSvc = new PegawaiSvc.PegawaiService();
            kehadiranConnector = new KehadiranConnector();
            izinConnector = new IzinConnector();
            lemburConnector = new LemburConnector();
            Util.ReadConfiguration();
        }

        private void LoadHadir()
        {
            try
            {
                this.dgDaftar.Items.Clear();
                presensi[] presensis = kehadiranConnector.Load();
                foreach (presensi p in presensis)
                {
                    Presensi ps = new Presensi();
                    ps.Nip = p.id.nip;
                    ps.Nama = pegawaiSvc.getById(ps.Nip).nama;
                    ps.Masuk = p.masuk.ToShortTimeString();
                    ps.Keluar = p.keluar.ToShortTimeString();

                    this.dgDaftar.Items.Add(ps);

                    ((DataGridTextColumn)this.dgDaftar.Columns[0]).Binding = new System.Windows.Data.Binding("Nip");
                    ((DataGridTextColumn)this.dgDaftar.Columns[1]).Binding = new System.Windows.Data.Binding("Nama");
                    ((DataGridTextColumn)this.dgDaftar.Columns[2]).Binding = new System.Windows.Data.Binding("Masuk");
                    ((DataGridTextColumn)this.dgDaftar.Columns[3]).Binding = new System.Windows.Data.Binding("Keluar");
                }
            }
            catch (Exception E)
            {
                System.Windows.MessageBox.Show("Terdapat Kesalahan!\n'" + E.Message + "'");
            }
        }

        private void LoadIzin()
        {
            try
            {
                this.dgDaftar.Items.Clear();
                izin[] izins = izinConnector.Load();
                foreach (izin i in izins)
                {
                    Izin iz = new Izin();
                    iz.Nip = i.id.nip;
                    iz.Nama = pegawaiSvc.getById(iz.Nip).nama;
                    iz.Keluar = i.keluar.ToShortTimeString();
                    iz.Masuk = i.masuk.ToShortTimeString();

                    this.dgDaftar.Items.Add(iz);

                    ((DataGridTextColumn)this.dgDaftar.Columns[0]).Binding = new System.Windows.Data.Binding("Nip");
                    ((DataGridTextColumn)this.dgDaftar.Columns[1]).Binding = new System.Windows.Data.Binding("Nama");
                    ((DataGridTextColumn)this.dgDaftar.Columns[2]).Binding = new System.Windows.Data.Binding("Keluar");
                    ((DataGridTextColumn)this.dgDaftar.Columns[3]).Binding = new System.Windows.Data.Binding("Masuk");
                }
            }
            catch (Exception E)
            {
                System.Windows.MessageBox.Show("Terdapat Kesalahan!\n'" + E.Message + "'");
            }
        }

        private void LoadLembur()
        {
            try
            {
                this.dgDaftar.Items.Clear();
                lembur[] lemburs = lemburConnector.Load();
                foreach (lembur l in lemburs)
                {
                    Lembur lm = new Lembur();
                    lm.Nip = l.id.nip;
                    lm.Nama = pegawaiSvc.getById(lm.Nip).nama;

                    this.dgDaftar.Items.Add(lm);

                    ((DataGridTextColumn)this.dgDaftar.Columns[0]).Binding = new System.Windows.Data.Binding("Nip");
                    ((DataGridTextColumn)this.dgDaftar.Columns[1]).Binding = new System.Windows.Data.Binding("Nama");
                }
            }
            catch (Exception E)
            {
                System.Windows.MessageBox.Show("Terdapat Kesalahan!\n'" + E.Message + "'");
            }
        }

        private void LoadPegawai()
        {
            try
            {
                pegawai[] pegawais = pegawaiSvc.getByDepartment(Util.DEPTID);

                this.dgData.IsEnabled = true;
                this.dgData.IsReadOnly = true;
                this.dgData.Items.Clear();

                foreach (pegawai p in pegawais)
                {
                    this.dgData.Items.Add(p);

                    ((DataGridTextColumn)this.dgData.Columns[0]).Binding = new System.Windows.Data.Binding("nip");
                    ((DataGridTextColumn)this.dgData.Columns[1]).Binding = new System.Windows.Data.Binding("nama");
                    ((DataGridTextColumn)this.dgData.Columns[2]).Binding = new System.Windows.Data.Binding("golongan");
                }
            }
            catch (Exception E)
            {
                System.Windows.MessageBox.Show("Terdapat Kesalahan!\n'" + E.Message + "'");
            }
        }

        private void btnMasuk_Click(object sender, RoutedEventArgs e)
        {
            String pesan = "";
            try
            {
                String nip = this.txtNip.Text;
                switch (operasi)
                {
                    case 0: pesan = kehadiranConnector.Masuk(nip);
                        LoadHadir();
                        break;
                    case 1: pesan = izinConnector.Masuk(nip);
                        LoadIzin();
                        break;
                    case 2: pesan = lemburConnector.Daftar(nip);
                        LoadLembur();
                        break;
                }
            }
            catch (Exception E)
            {
                pesan = "Terdapat Kesalahan!\n'" + E.Message + "'";
            }
            finally
            {
                System.Windows.MessageBox.Show(pesan);
            }
        }

        private void btnKeluar_Click(object sender, RoutedEventArgs e)
        {
            String pesan = "";
            try
            {
                String nip = this.txtNip.Text;
                switch (operasi)
                {
                    case 0: pesan = kehadiranConnector.Keluar(nip);
                        break;
                    case 1: pesan = izinConnector.Keluar(nip);
                        break;
                }
            }
            catch (Exception E)
            {
                pesan = "Terdapat Kesalahan!\n'" + E.Message + "'";
            }
            finally
            {
                System.Windows.MessageBox.Show(pesan);
            }
        }

        private void tabDaftar_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbJenisOperasi.Visibility = System.Windows.Visibility.Visible;
            this.lblJenisOperasi.Visibility = System.Windows.Visibility.Visible;
        }

        private void TabDataLoaded(object sender, RoutedEventArgs e)
        {
            this.cbJenisOperasi.Visibility = System.Windows.Visibility.Hidden;
            this.lblJenisOperasi.Visibility = System.Windows.Visibility.Hidden;

            DataGridTextColumn dgTextColumnNip = new DataGridTextColumn();
            DataGridTextColumn dgTextColumnNama = new DataGridTextColumn();
            DataGridTextColumn dgTextColumnGolongan = new DataGridTextColumn();
            dgTextColumnNip.Header = "NIP";
            dgTextColumnNama.Header = "NAMA";
            dgTextColumnGolongan.Header = "GOLONGAN";

            this.dgData.Columns.Clear();
            this.dgData.Columns.Add(dgTextColumnNip);
            this.dgData.Columns.Add(dgTextColumnNama);
            this.dgData.Columns.Add(dgTextColumnGolongan);

            LoadPegawai();
        }

        private void OperasiSelected(object sender, SelectionChangedEventArgs e)
        {
            this.txtNip.Text = "";
            operasi = this.cbJenisOperasi.SelectedIndex;

            this.dgDaftar.IsEnabled = true;
            this.dgDaftar.IsReadOnly = true;
            this.dgDaftar.Columns.Clear();
            this.dgDaftar.Items.Clear();
            switch (operasi)
            {
                case 0:
                    DataGridTextColumn hadirColumnNip = new DataGridTextColumn();
                    DataGridTextColumn hadirColumnNama = new DataGridTextColumn();
                    DataGridTextColumn hadirColumnMasuk = new DataGridTextColumn();
                    DataGridTextColumn hadirColumnKeluar = new DataGridTextColumn();
                    hadirColumnNip.Header = "NIP";
                    hadirColumnNama.Header = "NAMA";
                    hadirColumnMasuk.Header = "JAM MASUK";
                    hadirColumnKeluar.Header = "JAM KELUAR";
                    this.dgDaftar.Columns.Add(hadirColumnNip);
                    this.dgDaftar.Columns.Add(hadirColumnNama);
                    this.dgDaftar.Columns.Add(hadirColumnMasuk);
                    this.dgDaftar.Columns.Add(hadirColumnKeluar);
                    this.btnMasuk.Content = "Masuk";
                    this.btnMasuk.Visibility = System.Windows.Visibility.Visible;
                    this.btnKeluar.Visibility = System.Windows.Visibility.Visible;
                    LoadHadir();
                    break;
                case 1:
                    DataGridTextColumn izinColumnNip = new DataGridTextColumn();
                    DataGridTextColumn izinColumnNama = new DataGridTextColumn();
                    DataGridTextColumn izinColumnMasuk = new DataGridTextColumn();
                    DataGridTextColumn izinColumnKeluar = new DataGridTextColumn();
                    izinColumnNip.Header = "NIP";
                    izinColumnNama.Header = "NAMA";
                    izinColumnKeluar.Header = "JAM KELUAR";
                    izinColumnMasuk.Header = "JAM MASUK";
                    this.dgDaftar.Columns.Add(izinColumnNip);
                    this.dgDaftar.Columns.Add(izinColumnNama);
                    this.dgDaftar.Columns.Add(izinColumnKeluar);
                    this.dgDaftar.Columns.Add(izinColumnMasuk);
                    this.btnMasuk.Content = "Masuk";
                    this.btnMasuk.Visibility = System.Windows.Visibility.Visible;
                    this.btnKeluar.Visibility = System.Windows.Visibility.Visible;
                    LoadIzin();
                    break;
                case 2:
                    DataGridTextColumn lemburColumnNip = new DataGridTextColumn();
                    DataGridTextColumn lemburColumnNama = new DataGridTextColumn();
                    lemburColumnNip.Header = "NIP";
                    lemburColumnNama.Header = "NAMA";
                    this.dgDaftar.Columns.Add(lemburColumnNip);
                    this.dgDaftar.Columns.Add(lemburColumnNama);
                    this.btnMasuk.Content = "Lembur";
                    this.btnMasuk.Visibility = System.Windows.Visibility.Visible;
                    this.btnKeluar.Visibility = System.Windows.Visibility.Hidden;
                    LoadLembur();
                    break;
            }
        }

        private void btnCari_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                String nip = this.txtNipData.Text;
                PegawaiSvc.pegawai pegawai = pegawaiSvc.getById(nip);

                this.txtNama.Text = pegawai.nama;
                this.txtGolongan.Text = "";
            }
            catch (Exception E)
            {
                System.Windows.MessageBox.Show("Terdapat Kesalahan!\n'" + E.Message + "'");
            }
        }

        // Inner Classes
        private class KehadiranConnector
        {
            private PresensiSvc.PresensiService PresensiService;

            public KehadiranConnector()
            {
                PresensiService = new PresensiSvc.PresensiService();
            }

            public String Masuk(String nip)
            {
                try
                {
                    return PresensiService.masuk(nip, Util.DEPTID);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }

            public String Keluar(String nip)
            {
                try
                {
                    return PresensiService.keluar(nip, Util.DEPTID);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }

            public PresensiSvc.presensi[] Load()
            {
                try
                {
                    return PresensiService.getByDepartmentTanggal(Util.DEPTID, Util.Waktu.Day, Util.Waktu.Month, Util.Waktu.Year);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }
        }

        private class IzinConnector
        {
            private IzinSvc.IzinService IzinService;

            public IzinConnector()
            {
                IzinService = new IzinSvc.IzinService();
            }

            public String Masuk(String nip)
            {
                try
                {
                    return IzinService.masuk(nip, Util.DEPTID);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }

            public String Keluar(String nip)
            {
                try
                {
                    return IzinService.keluar(nip, Util.DEPTID);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }

            public IzinSvc.izin[] Load()
            {
                try
                {
                    return IzinService.getByDepartmentTanggal(Util.DEPTID, Util.Waktu.Day, Util.Waktu.Month, Util.Waktu.Year);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }
        }

        private class LemburConnector
        {
            private LemburSvc.LemburService LemburService;

            public LemburConnector()
            {
                LemburService = new LemburSvc.LemburService();
            }

            public String Daftar(String nip)
            {
                try
                {
                    return LemburService.daftar(nip, Util.DEPTID);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }

            public LemburSvc.lembur[] Load()
            {
                try
                {
                    return LemburService.getByDepartmentTanggal(Util.DEPTID, Util.Waktu.Day, Util.Waktu.Month, Util.Waktu.Year);
                }
                catch (Exception E)
                {
                    throw E;
                }
            }
        }

        public class Util
        {
            public static String DEPTID;

            public static void ReadConfiguration()
            {
                Configuration.read(Configuration.DefaultSetting);

                if (Configuration.KODE != null)
                {
                    DEPTID = Configuration.KODE;
                }
            }

            public static class Waktu
            {
                private static DateTime _TODAY = DateTime.Today;

                public static int Day
                {
                    get { return (_TODAY.Day); }
                }

                public static int Month
                {
                    get { return (_TODAY.Month); }
                }

                public static int Year
                {
                    get { return (_TODAY.Year); }
                }
            }
        }

    }
}
